<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;  // 从环境变量获取基础 URL

const router = useRouter()

const username = ref('')
const password = ref('')

const login = async () => {
  try {

    const response = await axios.post(`${apiBaseUrl}/login`, {
      'username': username.value,
      'password': password.value
    });


    if (response.data.status === true) {
      // 保存用户信息到本地存储
      localStorage.setItem('user', JSON.stringify({
        role: response.data.role,
        name: response.data.name,
        id:'waiter'
      }));


      // 根据角色进行不同的跳转
      switch(response.data.role) {
        case 'administrator':
          await router.push({ name: 'AdminConfig' });
          break;
        case 'waiter':
          await router.push({ name: 'WaiterConsole' });
          break;
        case 'manager':
          await router.push({ name: 'ManagerConsole' });
          break;
      }


    } else {
      alert(response.data.message); // 显示错误消息
    }

  } catch (error) {
    console.error('登录请求失败:', error);
    alert('登录请求失败!');
  }
}

const test = async () =>{
  try {
    const response = await axios.post(`${apiBaseUrl}/test/event_end`);

  } catch (error) {
    console.error('测试失败：', error);
    alert('测试失败');
  }
}
</script>

<template>
  <div class="base">

    <div class="login-container">
      <button @click="test()" ><span style="bottom: 80px">测试</span></button>
      <form @submit.prevent="login" style="top:0;">
      <div class="left-container">
        <div class="title">波普特智能酒店管理系统</div>
        <div class="input-container">
          <input v-model="username" type="text" name="username" placeholder="用户名">
          <input v-model="password" type="password" name="password" placeholder="密码">
        </div>
      </div>
      <div class="right-container">
        <div class="action-container">

          <button type="submit"><span>提交</span></button>
        </div>
      </div>
    </form>
    </div>
  </div>
</template>

<style scoped>
.base{
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%; /* Adjust width as needed */
  height: 100%; /* Adjust height as needed */
  display: flex;
  justify-content: center;
  margin:auto 0;
  background-color: #372e4a;
  //background-image: linear-gradient(to bottom right, rgb(114, 135, 254), rgb(130, 88, 186));

}
.login-container {
  position: fixed;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 600px; /* Adjust width as needed */
  height: 315px; /* Adjust height as needed */
  display: flex;
  justify-content: center;
  margin: 10% auto 0;
  border-radius: 15px;
  box-shadow:
      0px 0.9px 5.2px -43px rgba(0, 0, 0, 0.058),
      0px 3.8px 11.9px -43px rgba(0, 0, 0, 0.1),
      0px 10.3px 21.4px -43px rgba(0, 0, 0, 0.137),
      0px 25.3px 37.2px -43px rgba(0, 0, 0, 0.18),
      0px 74px 69px -43px rgba(0, 0, 0, 0.28)
;
  background-image: linear-gradient(to bottom right, rgb(118, 76, 163), rgb(92, 103, 211));

}
.left-container {
  display: inline-block;
  width: 330px;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
  padding: 60px;

}
.title {
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.title span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.input-container {
  padding: 20px 0;
}
input {
  border: 0;
  background: none;
  outline: none;
  color: #fff;
  margin: 20px 0;
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: .2s;
  border-bottom: 1px solid rgb(199, 191, 219);
}
input:hover {
  border-bottom-color: #fff;
}
::-webkit-input-placeholder {
  color: rgb(199, 191, 219);
}
.message-container {
  font-size: 14px;
  transition: .2s;
  color: rgb(199, 191, 219);
  cursor: pointer;
}
.message-container:hover {
  color: #fff;
}
.right-container {
  width: 145px;
  display: inline-block;
  height: calc(100% - 120px);
  vertical-align: top;
  padding: 60px 0;
}
.regist-container {
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.regist-container span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.action-container {
  font-size: 10px;
  color: #fff;
  text-align: center;
  position: relative;
  top: 200px;
}
.action-container span {
  background-color: mediumpurple;
  border: 1px solid rgb(237, 221, 22);
  padding: 10px;
  display: inline;
  line-height: 20px;
  border-radius: 20px;
  position: absolute;
  bottom: 10px;
  left: calc(72px - 20px);
  transition: .2s;
  cursor: pointer;
}
.action-container span:hover {
  background-color: rgb(237, 221, 22);
  color: midnightblue;
}
button {
  background-color: transparent;
  border: none;  /* 移除边框 */
  color: #fff;  /* 设置文字颜色 */
  cursor: pointer;  /* 设置鼠标为指针形状 */
}

</style>